package be.dch.typeditor.parser;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import be.dch.csv.CSVParser;
import be.dch.csv.mapper.LineMapper;
import be.dch.csv.processor.AbstractCellProcessor;
import be.dch.csv.processor.StringProcessor;
import be.dch.csv.validator.Optional;
import be.dch.csv.validator.RegExValidator;
import be.dch.csv.validator.Unique;
import be.dch.typeditor.domain.Line;

public final class ConfigEnumeration {

    private static final String LINE_CONFIG = "/be/dch/typeditor/garmin.osm.line.csv";

    private static ConfigEnumeration instance = new ConfigEnumeration();
    private final transient Map<String, Line> map = new HashMap<String, Line>();

    public static ConfigEnumeration getInstance() {
	return ConfigEnumeration.instance;
    }

    private ConfigEnumeration() {
	this.initLine();
    }

    private void initLine() { // FIXME
	final Set<AbstractCellProcessor> processors = new HashSet<AbstractCellProcessor>();
	processors.add(new StringProcessor(new Unique()));
	processors.add(new StringProcessor(new Optional(new RegExValidator(".*"))));
	processors.add(new StringProcessor());
	final CSVParser<Line> inFile = new CSVParser<Line>(Line.class, ConfigEnumeration.class.getResourceAsStream(ConfigEnumeration.LINE_CONFIG), ",", CSVParser.CUSTOM_ACCESSOR, processors);
	inFile.setMapper(new LineMapper());
	Line line = inFile.readLine();
	while (line != null) {
	    this.map.put(line.getI18nkey(), line);
	    line = inFile.readLine();
	}

    }

}
